package com.hdax.dm.controller;

import com.hdax.dm.common.exception.DmException;
import com.hdax.dm.common.exception.user.UserLoginException;
import com.hdax.dm.dto.CommonResponse;
import com.hdax.dm.dto.DtoUtil;
import com.hdax.dm.service.LinkUserService;
import com.hdax.dm.vo.TicketbuyerlistVo;
import com.hdax.dm.vo.addticketbuyerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class LinkUserController {

    @Autowired
    private LinkUserService linkUserService;
//    @Autowired
//    private RedisTemplate<String,String> redisTemplate;

    @PostMapping(path = "/ticketbuyerlist")             //根据用户id查询联系人信息
    public CommonResponse<List<TicketbuyerlistVo>> ticketbuyerlist(HttpServletRequest request) throws DmException {
        String token = request.getHeader("token");
        if (token == null || token.equals("")){
            throw new UserLoginException(UserLoginException.ERR_INFO.NO_LOGIN_MSG,
                    UserLoginException.ERR_INFO.NO_LOGIN_CODE);
        }
//        String s = token.substring(3,4);
        String s = token.split(":")[1].split("-")[0];
        return DtoUtil.returnSuccess("",linkUserService.ticketbuyerlist(Long.parseLong(s)));
    }

    @PostMapping(path = "/validatebuyerexist")
    public CommonResponse<String> validatebuyerexist(@RequestBody Map<String,String> params){
        String idCard = params.get("idCard");
        return DtoUtil.returnSuccess("",linkUserService.validatebuyerexist(idCard));
    }

    @PostMapping(path = "/addticketbuyer")
    public CommonResponse<String> addticketbuyer(@RequestBody addticketbuyerVo vo,HttpServletRequest request){
        String token = request.getHeader("token");
        String s = token.substring(3,4);
        vo.setUserId(Long.parseLong(s));
        return DtoUtil.returnSuccess("",linkUserService.addticketbuyer(vo));
    }

}
